home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Pict2Ascii 1.03 / Utils / PL_Utils.cp < prev    next >
Encoding:
Text File  |  1997-05-22  |  4.9 KB  |  156 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    PL_Utils.cp                                1997 BB's Team inc. All rights reserved
  3. // =================================================================================
  4.  
  5. #include "PL_Utils.h"
  6. #include <UDrawingState.h>
  7. #include <Sound.h>
  8.  
  9. unsigned char    PL_Utils::sBaseWidthChar    = 'W';
  10. unsigned char    PL_Utils::sVersusWidthChar    = 'i';
  11. unsigned char    PL_Utils::sWidthRepeat        =  6 ;
  12. unsigned char    PL_Utils::sMonoTestSize        =  9 ;
  13.  
  14. int PL_Utils_Init::count=0;
  15.  
  16. // ====================================================================
  17. // Initialize PL_Utils from rsrc
  18. // ====================================================================
  19. void PL_Utils::Initialize (void)
  20. {
  21.     Handle h = GetResource ('hide', 128);
  22.     if (h) {
  23.         sBaseWidthChar        = *(*h  );
  24.         sVersusWidthChar    = *(*h+1);
  25.         sWidthRepeat        = *(*h+2);
  26.         sMonoTestSize        = *(*h+3);
  27.         ReleaseResource (h);
  28.     }
  29. }
  30.  
  31.  
  32. // ====================================================================
  33. // Centers a Rect in another (bigger) one
  34. // ====================================================================
  35. void PL_Utils::CenterRect (Rect *toFit, const Rect &frame)
  36. {
  37.     // pin top-left corner to frame's one
  38.     ::OffsetRect (toFit, frame.left-toFit->left, frame.top-toFit->top);
  39.  
  40.     // add half the difference
  41.     ::OffsetRect (toFit, (frame.right-toFit->right)/2, (frame.bottom-toFit->bottom)/2);
  42. }
  43.  
  44.  
  45. // ====================================================================
  46. // find out the biggest rect proportionnal to toFit that fits in frame
  47. // ====================================================================
  48. void PL_Utils::FitRect (Rect *toFit, const Rect &frame)
  49. {
  50.     float ratioW = (float)(frame.right  - frame.left) / (toFit->right  - toFit->left);
  51.     float ratioH = (float)(frame.bottom - frame.top ) / (toFit->bottom - toFit->top );
  52.     if (ratioH < ratioW)
  53.         ::SetRect (toFit, toFit->left,
  54.                         toFit->top,
  55.                         toFit->left + (toFit->right  - toFit->left) * ratioH,
  56.                         toFit->top  + frame.bottom-frame.top);
  57.     else
  58.         ::SetRect (toFit, toFit->left,
  59.                         toFit->top,
  60.                         toFit->left + frame.right-frame.left,
  61.                         toFit->top  + (toFit->bottom - toFit->top ) * ratioW);
  62. }
  63.  
  64.  
  65. // ====================================================================
  66. // Counts the bits set in an unsigned char
  67. // ====================================================================
  68. int PL_Utils::CountBits (unsigned char u)
  69. {
  70.     int count = 0;
  71.     while (u) {
  72.         count++;
  73.         u &= u-1;
  74.     }
  75.     return count;
  76. }
  77.  
  78.  
  79. // ====================================================================
  80. // Play a named resource sound (system or in an open rsrc)
  81. // ====================================================================
  82. void PL_Utils::PlayNamedSound (Str255 name)
  83. {
  84.     Handle mySndHandle;
  85.     mySndHandle = GetNamedResource ('snd ', name);
  86.     if ( mySndHandle ) {
  87.         SndPlay (nil, (SndListHandle) mySndHandle, true);
  88.         ReleaseResource (mySndHandle);
  89.     }
  90. }
  91.  
  92.  
  93. // ---------------------------------------------------------------------------------
  94. // Compute a font's characteristics
  95. // ---------------------------------------------------------------------------------
  96. void PL_Utils::ComputeBBox (
  97.     TextTraitsRecord &    inTextTraits,
  98.     Int32                inSize,
  99.     Int32                &outWidth,
  100.     Int32                &outHeight,
  101.     Int32                &outAscent    )
  102. {
  103.     // auto-saves present font characteristics
  104.     StTextState localTextState;
  105.     UTextTraits::SetPortTextTraits (&inTextTraits);
  106.     ::TextSize(inSize);
  107.  
  108.     outWidth = ::CharWidth (sBaseWidthChar);
  109.  
  110.     FontInfo    fInfo;
  111.     ::GetFontInfo (&fInfo);
  112.     outAscent = fInfo.ascent; 
  113.     outHeight = fInfo.ascent + fInfo.descent;
  114. }
  115.  
  116.  
  117. // ---------------------------------------------------------------------------------
  118. // Returns the width of several times the same char in the current font/size
  119. // ---------------------------------------------------------------------------------
  120. Int16 PL_Utils::RepeatCharWidth (unsigned char inChar)
  121. {
  122.     static unsigned char buffer[255];
  123.     if (!inChar)
  124.         inChar = sBaseWidthChar;
  125.     for (int i=0 ; i<sWidthRepeat ; i++)
  126.         buffer[i]=inChar;
  127.     return ::TextWidth (buffer, 0, sWidthRepeat);
  128. }
  129.  
  130. // ---------------------------------------------------------------------------------
  131. // Guess if a font has a fixed width by comparing two char's repeated widths
  132. // ---------------------------------------------------------------------------------
  133. Boolean PL_Utils::IsMonoSpace (Str255 fontName)
  134. {
  135.     Int16 fontNumber;
  136.     StTextState saveState;
  137.     
  138.     ::GetFNum (fontName, &fontNumber);
  139.     ::TextFont (fontNumber);
  140.     ::TextSize (sMonoTestSize);
  141.  
  142.     return RepeatCharWidth (sBaseWidthChar) == RepeatCharWidth (sVersusWidthChar);
  143. }
  144.  
  145.  
  146. // ---------------------------------------------------------------------------------
  147. // Allocate a Handle in core or temp memory
  148. // ---------------------------------------------------------------------------------
  149. Handle PL_Utils::ForceNewHandle (Handle &h, Int32 size)
  150. {
  151.     h = ::NewHandle (size);
  152.     if (h == nil)
  153.         h = ::NewHandleSys (size);
  154.     return h;
  155. }
  156.